package org.example.coupon.service.impl;

import org.example.coupon.dao.SkuFullReductionDao;
import org.example.coupon.dao.SkuLadderDao;
import org.example.coupon.entity.SkuFullReductionEntity;
import org.example.coupon.entity.SkuLadderEntity;
import org.example.coupon.service.CouponPreferentialService;
import org.example.coupon.to.PreferentialTo;
import org.example.coupon.vo.PreferentialVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Service
public class CouponPreferentialServiceImpl implements CouponPreferentialService {

    @Autowired
    private SkuLadderDao skuLadderDao;
    @Autowired
    private SkuFullReductionDao skuFullReductionDao;

    // 保存优惠信息
    @Override
    public void addPreferential(PreferentialVo preferentialVo) {
        if (preferentialVo.getSkuLadder() != null && preferentialVo.getSkuLadder().size()>0){
            skuLadderDao.addSkuLadder(preferentialVo.getSkuLadder());
        }
        if (preferentialVo.getSkuFullReduction() != null && preferentialVo.getSkuFullReduction().size()>0){
            skuFullReductionDao.addSkuFullReduction(preferentialVo.getSkuFullReduction());
        }
    }

    // 查询优惠信息
    @Override
    public Map<Long, PreferentialTo> getPreferential(List<Long> skuIds) {
        Map<Long, PreferentialTo> toMap = new HashMap<>();
        if (skuIds != null){
            for (Long skuId : skuIds) {
                PreferentialTo preferentialTo = new PreferentialTo();
                List<SkuLadderEntity> skuLadder = skuLadderDao.getCartSkuLadder(skuId);
                preferentialTo.setLadders(skuLadder);
                List<SkuFullReductionEntity> skuFull = skuFullReductionDao.getCartSkuFull(skuId);
                preferentialTo.setFullReductions(skuFull);
                toMap.put(skuId, preferentialTo);
            }
        }
        return toMap;
    }
}
